package com.app.Activity;

import java.io.File;

import it.unipr.ce.dsg.s2p.sip.Address;

import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import app.com.ModuleRole.R;

public class frmNotificationMoiVaoNhom extends Activity {
	Button btnDongY;
	Button btnTuChoi;
	TextView txtLoiMoi;
	public static Runnable raFinish;
	public static Runnable raStarNhomCho;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_moi_vao_nhom);

		txtLoiMoi=(TextView)findViewById(R.id.txtLoiMoi);
		btnDongY = (Button) findViewById(R.id.btnDongY);
		btnTuChoi = (Button) findViewById(R.id.btnTuChoi);

		btnDongY.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {				
				ReadWriteFileController.deleteFile(ReadWriteFileController.MEMBER_PATH);
				NetworkController.sendDongYVaoNhom(new Address(NetworkController.sender));
				ReadWriteFileController.deleteFile(ReadWriteFileController.MY_ROLE_PATH);
				NetworkController.resetRole();
				//gán địa chỉ của trưởng nhóm
				NetworkController.thanhLapNhomIP=NetworkController.sender.toString();
				Log.d("Phu","da set ip thanh lap nhom="+NetworkController.thanhLapNhomIP);
				// finish();
			}
		});
		btnTuChoi.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				myFinish();				
			}
		});
		NotificationManager mn = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		mn.cancel(getIntent().getExtras().getInt("ID"));
		
		raFinish = new Runnable() {
			@Override
			public void run() {
				myFinish();
			}
		};
		raStarNhomCho = new Runnable() {
			@Override
			public void run() {
				Intent i = new Intent(getBaseContext(), frmNhomChoActivity.class);
				if(NetworkController._peer.account!=null)
					NetworkController._peer.account.isThanhLapNhom=false;
				NetworkController._peer.account.inGroup=true;
				startActivity(i);
				myFinish();
			}
		};
		Intent intent=getIntent();
		if(intent!=null){
			
			String tickerText, contentTitle,contentText;
			
			tickerText=intent.getStringExtra("tickerText");
			contentTitle=intent.getStringExtra("contentTitle");
			contentText=intent.getStringExtra("contentText");
			Log.i("Phu","trong inaskdkd "+tickerText+" : "+contentText+" : "+contentTitle);
			if(tickerText!=null){
				String []m=contentText.split("#~");
				//txtLoiMoi.setText(tickerText+"\n"+m[0]+"\n"+m[1]+"\n"+contentTitle);
				String msg=m[0]+"mời bạn vào nhóm: "+m[1]+"\n"+contentTitle;
				txtLoiMoi.setText(msg);
				NetworkController.setGroupName(m[1]);
				Log.d("Phu", "GroupName 0: " + NetworkController.getGroupName());
			//	txtLoiMoi.setText(tickerText+"\n"+contentTitle+"\n"+contentText);
			}
		}
		else{
			finish();
		}
	}

	private void myFinish() {
		this.finish();
	}
}
